package com.tuling.jucdemo.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author :tangweiliang
 * @date : 2022/11/17 21:02
 * 未来的你会感谢现在努力的自己
 *
 * 可中断
 */
@Slf4j
public class ReentrantLockDemo3 {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();

        Thread t1 = new Thread(() -> {
            log.debug("t1启动...");
            try {
                lock.lockInterruptibly();
                try {
                    log.debug("t1获得了锁...");
                } finally {
                    lock.unlock();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("t1等锁的过程被中断...");
            }
        }, "t1");

        lock.lock();
        try {
            log.debug("main线程获取了锁...");
            t1.start();
            //先让线程t1执行
            Thread.sleep(2000);
            t1.interrupt();
            log.debug("t1线程执行中断...");
        } finally {
            lock.unlock();
        }
    }
}
